import {abstractValidator} from "../validator/abstractValidator";
import {validatorEnum} from "../validator/validatorEnum";
import {validatorManage} from "../validator/validatorManage";


/**
 * Created by ybjaa on 2017/7/3.
 * @param validatorEnumType 验证类型
 * @param messagePrefix 验证信息前缀
 * @param obj 验证所需参数
 * @returns {(target:any, propertyName:string)=>undefined}
 */
export function setValidator(validatorEnumType:validatorEnum,messagePrefix:string="",...obj:any[]){


    return function (target:any,propertyName:string) {

        if(!target[Symbol.for("validator")]){
            target[Symbol.for("validator")]=new Map<Object, Map<string,abstractValidator[]>>();
        }

        let map:Map<Object, Map<string,abstractValidator[]>> =target[Symbol.for("validator")];

        let targetMap:Map<string,abstractValidator[]> =null;


        let validator=validatorManage.GetValidatorClasses(validatorEnumType);

        validator.setMessagePrefix(messagePrefix);
        validator.setParameters.apply(validator,obj);

        //targetMap=map.has(target)?map.get(target):new Map();

        if(!map.has(target)){
            targetMap=new Map();
            targetMap.set(propertyName,[validator]);
            map.set(target,targetMap);
            return;
        }else {
            targetMap=map.get(target);
        }


        if(!targetMap.has(propertyName)){
            targetMap.set(propertyName,[validator]);
        }else {
            targetMap.get(propertyName).push(validator);
        }


    }
    

}